package com.ly.algorithm.leetcode.dp;

/**
 * @Classname Problem746
 * @Description
 *
 * 输入: cost = [10, 15, 20]
 * 输出: 15
 * 解释: 最低花费是从cost[1]开始，然后走两步即可到阶梯顶，一共花费15。
 *  示例 2:
 *
 * 输入: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
 * 输出: 6
 * 解释: 最低花费方式是从cost[0]开始，逐个经过那些1，跳过cost[3]，一共花费6。
 *
 * @Date 2020/12/24 21:23
 * @Author 冷心影翼
 */
public class Problem746 {

	public static void main(String[] args) {
		Solution746 solution746 = new Solution746();
		System.out.println(solution746.minCostClimbingStairs(new int[]{1, 100, 1, 1, 1, 100, 1, 1, 100, 1}));
	}

}

class Solution746 {

	public int minCostClimbingStairs(int[] cost) {
		int[] dp = new int[cost.length];
		dp[0] = cost[0];
		dp[1] = cost[1];
		for (int i = 2; i < cost.length; i++) {
			dp[i] = Math.min(dp[i-1],dp[i-2])+cost[i];
		}
		return Math.min(dp[cost.length-1],dp[cost.length-2]);
	}

	public int minCostClimbingStairs2(int[] cost) {
		int n = cost.length;
		int[] dp = new int[n + 1];
		dp[0] = dp[1] = 0;
		for (int i = 2; i <= n; i++) {
			dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
		}
		return dp[n];
	}

}